/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#ifndef VOMPREMOTE_H
#define VOMPREMOTE_H

#include <QObject>
#include <QtNetwork>
#include <QSettings>
#include <QQmlListProperty>
#include <QQmlApplicationEngine>

#include "mvplist.h"
#include "mvplistmodel.h"
#include "channellist.h"
#include "channellistmodel.h"
#include "timerlistmodel.h"
#include "vdrfindserver.h"


const static int MVPPORT = 2000;

class VompRemote : public QObject
{
    Q_OBJECT
    QThread m_workerThread;

    Q_PROPERTY( int currentHost READ getCurrentHost WRITE setCurrentHost NOTIFY currentHostChanged )
    Q_PROPERTY( QString currentHostName READ getCurrentHostName NOTIFY currentHostChanged )

    Q_PROPERTY( bool colorButtonStyle READ getColorButtonStyle WRITE setColorButtonStyle NOTIFY colorButtonStyleChanged )

    Q_PROPERTY( bool vdrEnabled READ getVdrEnabled WRITE setVdrEnabled NOTIFY vdrEnabledChanged )
    Q_PROPERTY( bool vdrConnected READ getVdrConnected WRITE setVdrConnected NOTIFY vdrConnectedChanged )

    Q_PROPERTY( QString serverName READ getServerName WRITE setServerName NOTIFY serverNameChanged )
    Q_PROPERTY( QString serverAddress READ getServerAddress WRITE setServerAddress NOTIFY serverAddressChanged )

public:
    VompRemote( QObject *parent = 0 );
    ~VompRemote();

    VDR* vdr() { return m_vdr; }

    //MVP List
    MvpList* mvpList() { return &m_mvpList; }
    MvpListModel* mvpListModel() { return m_mvpListModel; }

    //Channel List
    ChannelList* channelList() { return &m_channelList; }
    ChannelListModel* channelListModel() { return m_channelListModel; }

    //Timer List
    QList<TimerEntry> * timerList() { return &m_timerList; }
    TimerListModel* timerListModel() { return m_timerListModel; }

    QDateTime serverTime();

    void connectSignals();

    void setCurrentHost( int pos );
    int getCurrentHost() const { return m_currentHost; }

    QString getCurrentHostName();

    void setColorButtonStyle( bool style );
    bool getColorButtonStyle() const { return m_colorButtonStyle; }

    void setVdrEnabled( bool vdrEnabled );
    bool getVdrEnabled() const { return m_vdrEnabled; }

    void setVdrConnected( bool vdrConnected );
    bool getVdrConnected() const { return m_vdr->state() == QAbstractSocket::ConnectedState; }

    QString getServerName() const { return m_vdrName; }
    void setServerName( QString name );

    QString getServerAddress() const { return m_vdrAddress.toString(); }
    void setServerAddress( QString address );

    void setRootObject( QObject *rootObject );
    void setEngine( QQmlApplicationEngine * engine ) { m_engine = engine; }


public slots:
    void sendKey( int key );
    void appendHost( QString name, QString host );
    void removeHost( int pos );

    void updateChannels( const ChannelList & channelList );
    void updateTimers( const QList<TimerEntry> & timerList );

    void searchVdr();
    void vdrConnectionStateChanged();


signals:
    void keySent();
    void messageError( const QString header, const QString message );

    void currentHostChanged();
    void currentHostNameChanged();
    void colorButtonStyleChanged();

    //VDR stati
    void vdrEnabledChanged();
    void vdrConnectedChanged();

    void getChannelList( quint32 );
    void getTimerList();

    void serverNameChanged();
    void serverAddressChanged();

private:
    void loadSettings();
    void saveSettings();
    bool loginVDR();
    void logoutVDR();

    bool searchForServer();

    QObject*     m_rootObject;

    QUdpSocket*  m_udpSocket;
    QSettings*   m_settings;
    int          m_currentHost;
    bool         m_colorButtonStyle;

    bool                    m_vdrEnabled;
    VDR *                   m_vdr;
    VDRFindServer *         m_vdrFindServer;
    QQmlApplicationEngine * m_engine;

    ChannelListModel *     m_channelListModel;
    ChannelList            m_channelList;
    TimerListModel *       m_timerListModel;
    QList<TimerEntry>      m_timerList;

    MvpListModel *         m_mvpListModel;
    MvpList                m_mvpList;

    QList< serverEntry >   m_serverList;

    QTimer*      m_timer;

    QHostAddress m_vdrAddress;
    QString      m_vdrName;

    qint32       m_serverTimeOffset;
};


#endif // VOMPREMOTE_H
